//Download by http://www.NewXing.com
#include"stdafx.h"
#include"CText.h"
#include "EastDrawView.h"
#include"TextEdit.h"
IMPLEMENT_SERIAL(CText,CUnit,1)
	CText::CText()
{
	CUnit::Initial();
	m_Font=new CFont;
	m_Font->CreatePointFont (100,_T("Arial"));
	m_pTextEdit = new CTextEdit;
	m_HaveFinish=false;
}
CText::~CText()
{
	delete m_Font;
	//delete m_pTextEdit;
}
void CText::Serialize(CArchive &ar)
{
	CUnit::Serialize(ar);

	LOGFONT m_lf; 


	if(ar.IsStoring())
	{
		m_Font->GetLogFont(&m_lf);

		ar<<m_Hight<<m_Width<<m_PositionPoint<<m_String;
		ar<<m_lf.lfCharSet;
		ar<<m_lf.lfClipPrecision;
		ar<<m_lf.lfEscapement;
		ar<<m_lf.lfHeight;
		ar<<m_lf.lfItalic;
		ar<<m_lf.lfOrientation;
		ar<<m_lf.lfOutPrecision;
		ar<<m_lf.lfPitchAndFamily;
		ar<<m_lf.lfQuality;	
		ar<<m_lf.lfWidth;
		ar<<m_lf.lfWeight;
		ar<<m_lf.lfUnderline;
		ar<<m_lf.lfStrikeOut;
		CString str=m_lf.lfFaceName;
		ar<<str;
	}
	else
	{
		m_HaveFinish=true;
		ar>>m_Hight>>m_Width>>m_PositionPoint>>m_String;

		ar>>m_lf.lfCharSet;
		ar>>m_lf.lfClipPrecision;
		ar>>m_lf.lfEscapement;
		ar>>m_lf.lfHeight;
		ar>>m_lf.lfItalic;
		ar>>m_lf.lfOrientation;
		ar>>m_lf.lfOutPrecision;
		ar>>m_lf.lfPitchAndFamily;
		ar>>m_lf.lfQuality;	
		ar>>m_lf.lfWidth;
		ar>>m_lf.lfWeight;
		ar>>m_lf.lfUnderline;
		ar>>m_lf.lfStrikeOut;
		CString str;
		ar>>str;
		::lstrcpy(m_lf.lfFaceName,str);
		m_Font->DeleteObject();
		m_Font->CreateFontIndirect(&m_lf);

		m_bHaveCreat=false;
	}
}

void CText::OnLButtonDown(CDC *pDC, CEastDrawView *pView, CPoint point)
{

	//if(m_HaveFindFirst)
	//{
	//	pView->L_iPointCount=IsOnMarginPoint(point);
	//	if(pView->L_iPointCount!=0)
	//	{
	//		pView->m_bHaveFindSecond=true;
	//		pView->m_CurrentDrawTool=Text_Tool;
	//		pView->m_CurrentDrawStatus=Change_Status;
	//		switch(pView->L_iPointCount)
	//		{
	//		case 1:
	//			pView->m_FirstPoint=m_SecondPoint;
	//			pView->m_SecondPoint=point;
	//			break;
	//		case 2:
	//			pView->m_FirstPoint=CPoint(m_FirstPoint.x,m_SecondPoint.y);
	//			pView->m_SecondPoint=point;
	//			break;
	//		case 3:
	//			pView->m_FirstPoint=m_FirstPoint;
	//			pView->m_SecondPoint=point;
	//			break;
	//		case 4:
	//			pView->m_FirstPoint=CPoint(m_SecondPoint.x,m_FirstPoint.y);
	//			pView->m_SecondPoint=point;
	//			break;
	//		case 5:
	//			pView->m_FirstPoint=m_FirstPoint;
	//			pView->m_SecondPoint=m_SecondPoint;
	//			pView->m_CurrentDrawTool=Text_Tool;
	//			pView->m_CurrentDrawStatus=Drag_Status;
	//			break;
	//		}
	//	}//******if(L_iPointCount!=0)
	//}//********if(L_pRectangle->m_HaveFindFirst)

	//if(!pView->m_bHaveFindSecond&&IsInRgn(point))
	//{
	//	if(!m_HaveFindFirst)
	//	{
	//		m_HaveFindFirst=true;
	//		pView->m_bHaveFindFirst=true;
	//		ShowSelectPoint(pDC);
	//	}
	//	else
	//	{
	//		m_HaveFindFirst=false;
	//		pView->m_bHaveFindFirst=true;
	//		ShowSelectPoint(pDC);
	//	}
	//}

	//if(pView->m_bHaveFindSecond)
	//{   
	//	pView->m_pCurrentUnit=this;
	//	m_DrawingMode=pDC->GetROP2();
	//	ShowSelectPoint(pDC);
	//	DrawOldReferencePoint(pDC,m_FoundPoint);
	//	DrawStatic(pDC);
	//}


	//if(pView->m_CurrentDrawStatus==Draw_Status)
	//{
	if(pView->m_LBDownTimes==2&&!m_pTextEdit->m_IsMoving)
	{
		CPoint point1(0,0);
		CPoint point2(abs(m_SecondPoint.x-m_FirstPoint.x),abs(m_SecondPoint.y-m_FirstPoint.y));
		m_pTextEdit->Create(ES_MULTILINE | ES_WANTRETURN | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,
			CRect(point1,point2), pView, 1);
		CPoint textEditPosition=CPoint(min(m_FirstPoint.x,m_SecondPoint.x),min(m_FirstPoint.y,m_SecondPoint.y));
		this->SetTextEditPosition(pDC,pView,textEditPosition);
		m_pTextEdit->ShowWindow(true);
		m_pTextEdit->SetFocus();

		pView->ShowFontBar(true);
		pView->SetBoxFont();

	}

	if(pView->m_LBDownTimes==3&&!m_pTextEdit->m_IsMoving)
	{
		pView->ShowFontBar(false);
		m_pTextEdit->UpdateData();
		m_pTextEdit->ShowWindow(false);
		m_HaveFinish=true; 
		m_pTextEdit->GetWindowText(m_String);
		pView->m_LBDownTimes=0;
		pView->ExchangeSave();


		if(this->m_DrawStatus==Change_Status)
		{
			this->m_DrawStatus=Draw_Status;
			pView->m_CurrentDrawStatus=Select_Status;
		}

		int nLineCount = m_pTextEdit->GetLineCount();
		CString strText;
		CFont *pOldFond=pDC->SelectObject(m_Font);
		int pOldBkMode=pDC->SetBkMode(this->m_BkMode);
		COLORREF oldBKColor=pDC->SetBkColor(this->m_BackgroundColor);
		COLORREF oldPenColor=pDC->SetTextColor(this->m_PenColor);
		CBrush brush;
		LOGBRUSH brushlog;
		brushlog.lbColor=m_BrushColor;
		brushlog.lbHatch=m_BrushHatch;
		brushlog.lbStyle=m_BrushStyle;
		brush.CreateBrushIndirect(&brushlog);
		CBrush*pOldBrush=pDC->SelectObject(&brush);
		TEXTMETRIC tm;
		pDC->GetTextMetrics(&tm);
		int maxLength=0;
		//int maxLineLong;

		for(int i=0;i<nLineCount;i++)
		{
			maxLength=maxLength>m_pTextEdit->LineLength(m_pTextEdit->LineIndex(i))?maxLength:m_pTextEdit->LineLength(m_pTextEdit->LineIndex(i));
			int ll=m_pTextEdit->LineLength(m_pTextEdit->LineIndex(i));
			if(ll==0) {
				continue;
			}

			m_pTextEdit->GetLine(0,strText.GetBuffer(ll),ll);

			strText.ReleaseBuffer();
			pDC->TextOut(m_PositionPoint.x,m_PositionPoint.y+i*(tm.tmHeight+tm.tmExternalLeading),strText.Left(m_pTextEdit->LineLength(m_pTextEdit->LineIndex(i))));

			strText.Empty();

		}

		this->m_Width=maxLength*(tm.tmMaxCharWidth);
		this->m_Hight=(nLineCount+1)*(tm.tmHeight+tm.tmExternalLeading);
		CRect rect;
		m_pTextEdit->GetWindowRect(&rect);
		m_Width=m_Width>rect.Width()?m_Width:rect.Width();
		m_Hight=m_Hight>rect.Height()?m_Hight:rect.Height();

		pDC->SetBkMode(pOldBkMode);
		pDC->SelectObject(pOldFond);
		pDC->SetBkColor(oldBKColor);
		pDC->SetTextColor(oldPenColor);
		pDC->SelectObject(pOldBrush);
	}
	if(pView->m_LBDownTimes==3&&m_pTextEdit->m_IsMoving)
	{
		pView->ShowFontBar(true);
		pView->m_LBDownTimes=2;

		pView->ExchangeSave();


		m_PositionPoint=point;
		m_pTextEdit->m_PositionPoint=point;
		CRect rect;
		m_pTextEdit->GetWindowRect(&rect);
		pDC->DPtoLP(&rect);
		pDC->DrawFocusRect(CRect(m_PositionPoint.x+1,m_PositionPoint.y+1,m_PositionPoint.x+rect.Width()-1,m_PositionPoint.y+rect.Height()-1));
		this->SetTextEditPosition(pDC,pView,m_PositionPoint);
		this->m_pTextEdit->m_IsMoving=false;
	}



	//if(pView->m_CurrentDrawStatus==Change_Status)
	//{
	//	pView->m_CurrentDrawStatus=Draw_Status;
	//	pView->m_LBDownTimes=2;

	//}//********if(pView->m_CurrentDrawStatus==Exchange_Status)

	/*if(pView->m_CurrentDrawStatus==Select_Status)
	{*/
	if(m_HaveFindFirst)
	{
		pView->L_iPointCount=IsOnMarginPoint(point);
		if(pView->L_iPointCount!=0)
		{
			pView->ShowFontBar(true);
			this->m_HaveFindFirst=false;
			pView->m_bHaveFindSecond=true;
			this->ShowSelectPoint(pDC);
			CPoint point=m_PositionPoint;
			pDC->LPtoDP(&point);

			CRect rect;
			m_pTextEdit->GetWindowRect(&rect);
			m_Width=m_Width>rect.Width()?m_Width:rect.Width();
			m_Hight=m_Hight>rect.Height()?m_Hight:rect.Height();
			this->m_pTextEdit->MoveWindow(point.x,point.y,m_Width,m_Hight);
			this->m_pTextEdit->ShowWindow(true);
			this->m_pTextEdit->SetFont(m_Font);
			pView->m_CurrentDrawTool=Text_Tool;
			pView->m_CurrentDrawStatus=Change_Status;
			pView->m_pCurrentUnit=this;
			pView->m_LBDownTimes=0;
			m_HaveFinish=false;
			this->m_DrawStatus=Change_Status;
		}
	}
	if(!pView->m_bHaveFindSecond&&IsInRgn(point))
	{
		if(!m_HaveFindFirst)
		{
			m_HaveFindFirst=true;
			pView->m_bHaveFindFirst=true;
			ShowSelectPoint(pDC);
		}
		else
		{
			m_HaveFindFirst=false;
			pView->m_bHaveFindFirst=true;
			ShowSelectPoint(pDC);
		}

	}
	//}//******if(pView->m_CurrentDrawStatus==Select_Status)
}


void CText::SetTextEditPosition(CDC *pDC, CEastDrawView *pView, CPoint point)
{
	CRect rect;
	m_pTextEdit->GetWindowRect(&rect);



	m_PositionPoint=point;
	m_pTextEdit->m_PositionPoint=point;
	this->m_Width=rect.Width();
	this->m_Hight=rect.Height();
	pDC->LPtoDP(&point);
	m_pTextEdit->m_PositionPoint=point;
	m_pTextEdit->MoveWindow(point.x,point.y,rect.Width(),rect.Height());
}

void CText::SetFont(ENUMLOGFONT *lpelf)
{
	m_pTextEdit->SetMyFont(m_Font);
}

void CText::ChangeFont(ENUMLOGFONT *lpelf)
{

	m_Font->DeleteObject();
	m_Font->CreateFontIndirect(&lpelf->elfLogFont);
	SetFont(lpelf);
}

void CText::ChangeFontSize(ENUMLOGFONT *lpelf)
{
	m_Font->DeleteObject();
	m_Font->CreateFontIndirect(&lpelf->elfLogFont);
	SetFont(lpelf);
}

void CText::DrawStatic(CDC *pDC)
{
	if(this->m_HaveFinish)
	{
		CMDIChildWnd*pChild=((CMDIFrameWnd*)AfxGetMainWnd())->MDIGetActive();
		CEastDrawView*pView=(CEastDrawView*)pChild->GetActiveView();
		if(!m_bHaveCreat)
		{
			m_pTextEdit->DestroyWindow();
			m_pTextEdit->Create(WS_VSCROLL|WS_HSCROLL|ES_MULTILINE | ES_WANTRETURN | WS_CHILD |WS_TABSTOP | WS_BORDER,
				CRect(m_PositionPoint,CPoint(m_PositionPoint.x+m_Width,m_PositionPoint.y+m_Hight)), pView, 1);
			m_bHaveCreat=true;
		}
		m_pTextEdit->SetWindowText(m_String);
		int nLineCount = m_pTextEdit->GetLineCount();
		CString strText;
		CFont *pOldFond=pDC->SelectObject(m_Font);
		int pOldBkMode=pDC->SetBkMode(this->m_BkMode);
		COLORREF oldBKColor=pDC->SetBkColor(this->m_BackgroundColor);
		COLORREF oldPenColor=pDC->SetTextColor(this->m_PenColor);
		CBrush brush;
		LOGBRUSH brushlog;
		brushlog.lbColor=m_BrushColor;
		brushlog.lbHatch=m_BrushHatch;
		brushlog.lbStyle=m_BrushStyle;
		brush.CreateBrushIndirect(&brushlog);
		CBrush*pOldBrush=pDC->SelectObject(&brush);

		TEXTMETRIC tm;
		pDC->GetTextMetrics(&tm);

		for(int i=0;i<nLineCount;i++)
		{
			int ll=m_pTextEdit->LineLength(m_pTextEdit->LineIndex(i));
			if(ll==0){
				continue;
			}

			m_pTextEdit->GetLine(i,strText.GetBuffer(ll),ll);
			strText.ReleaseBuffer();
			pDC->TextOut(m_PositionPoint.x,m_PositionPoint.y+i*(tm.tmHeight+tm.tmExternalLeading),strText.Left(m_pTextEdit->LineLength(m_pTextEdit->LineIndex(i))));
			strText.Empty();
		}
		pDC->SetBkMode(pOldBkMode);
		pDC->SelectObject(pOldFond);
		pDC->SetBkColor(oldBKColor);
		pDC->SetTextColor(oldPenColor);
		pDC->SelectObject(pOldBrush);
	}

}

void CText::OnMouseMove(CDC *pDC, CEastDrawView *pView, CPoint point)
{

	CString s;
	s.Format(_T("OnMouseMove m_LBDownTimes  %d m_IsCreate %d \n\r"),pView->m_LBDownTimes,m_pTextEdit->m_IsCreate);
	OutputDebugString(s);





	if(pView->m_CurrentDrawStatus==Draw_Status)
	{
		if(!m_pTextEdit->m_IsCreate){

			pDC->SetROP2(R2_NOTXORPEN);
			this->DrawActive(pDC,point);
			pView->m_SecondPoint=point;
			m_SecondPoint=pView->m_SecondPoint;
			this->DrawActive(pDC,point);

			if(pView->m_LBDownTimes==2&&!m_pTextEdit->m_IsMoving){

				this->DrawActive(pDC,point);
				m_pTextEdit->m_IsCreate=true;
				return;
			}

			if(pView->m_LBDownTimes==3&&!m_pTextEdit->m_IsMoving){
				return;
			}
		}
	}


	if(m_pTextEdit!=NULL&&m_pTextEdit->m_IsMoving)
	{
		CRect rect;
		m_pTextEdit->GetWindowRect(&rect);
		pDC->DPtoLP(&rect);
		CPen pen;
		pen.CreatePen(PS_SOLID,2,RGB(255,0,0));
		CPen*pOldPen=pDC->SelectObject(&pen);
		//this->SetTextEditPosition(pDC,pView,point);

		pDC->DrawFocusRect(CRect(m_PositionPoint.x+1,m_PositionPoint.y+1,m_PositionPoint.x+rect.Width()-1,m_PositionPoint.y+rect.Height()-1));
		m_PositionPoint=point;

		pDC->DrawFocusRect(CRect(m_PositionPoint.x+1,m_PositionPoint.y+1,m_PositionPoint.x+rect.Width()-1,m_PositionPoint.y+rect.Height()-1));
		pDC->SelectObject(pOldPen);
	}
}

void CText::OnMenuitemOk(CDC *pDC, CEastDrawView *pView)
{
	if(m_pTextEdit->m_IsMoving)
	{
		CRect rect;
		m_pTextEdit->GetWindowRect(&rect);
		pDC->DPtoLP(&rect);
		pDC->DrawFocusRect(CRect(m_PositionPoint.x+1,m_PositionPoint.y+1,m_PositionPoint.x+rect.Width()-1,m_PositionPoint.y+rect.Height()-1));
		SetTextEditPosition(pDC,pView,m_PositionPoint);
		m_pTextEdit->m_IsMoving=false;
	}

}

BOOL CText::IsInRgn(CPoint point)
{
	CPoint FirstPoint;
	CPoint SecondPoint;
	FirstPoint.x=m_FirstPoint.x<m_SecondPoint.x?m_FirstPoint.x:m_SecondPoint.x;
	FirstPoint.y=m_FirstPoint.y<m_SecondPoint.y?m_FirstPoint.y:m_SecondPoint.y;
	SecondPoint.x=m_FirstPoint.x>m_SecondPoint.x?m_FirstPoint.x:m_SecondPoint.x;
	SecondPoint.y=m_FirstPoint.y>m_SecondPoint.y?m_FirstPoint.y:m_SecondPoint.y;
	m_FirstPoint=FirstPoint;
	m_SecondPoint=SecondPoint;

	CRect rect1(m_FirstPoint,m_SecondPoint);
	rect1.InflateRect(4,4);
	CRect rect2(m_FirstPoint,m_SecondPoint);
	rect2.DeflateRect(4,4);
	return (rect1.PtInRect(point)&&!rect2.PtInRect(point));
}

int CText::IsOnMarginPoint(CPoint point)
{
	CRect rect(point,point);
	rect.InflateRect(CSize(10,10));
	if(rect.PtInRect(m_FirstPoint))
	{   
		m_FoundPoint=m_FirstPoint;
		return 1;
	}
	if(rect.PtInRect(CPoint(m_SecondPoint.x,m_FirstPoint.y)))
	{   
		m_FoundPoint=(CPoint(m_SecondPoint.x,m_FirstPoint.y));
		return 2;
	}
	if(rect.PtInRect(m_SecondPoint))
	{	 
		m_FoundPoint=m_SecondPoint;
		return 3;
	}
	if(rect.PtInRect(CPoint(m_FirstPoint.x,m_SecondPoint.y)))
	{  
		m_FoundPoint=CPoint(m_FirstPoint.x,m_SecondPoint.y);
		return 4;
	}
	if(rect.PtInRect(CPoint((m_FirstPoint.x+m_SecondPoint.x)/2,(m_FirstPoint.y+m_SecondPoint.y)/2)))
	{  
		m_FoundPoint=CPoint((m_FirstPoint.x+m_SecondPoint.x)/2,(m_FirstPoint.y+m_SecondPoint.y)/2);
		return 5;
	}
	return 0;

}

void CText::ShowSelectPoint(CDC *pDC)
{


	CBrush brush;
	brush.CreateSolidBrush(RGB(0,0,255));
	CPen m_pen;
	m_pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
	CPen *OldPen=pDC->SelectObject(&m_pen);
	int oldBkMode=pDC->SetBkMode(OPAQUE); 
	CBrush *OldBrush=pDC->SelectObject(&brush);
	int oldDrawingMode=pDC->SetROP2(R2_NOTXORPEN);
	CRect rect;

	rect.SetRect(this->m_FirstPoint,this->m_FirstPoint);
	rect.InflateRect(3,3);
	pDC->Rectangle(rect);

	rect.SetRect(this->m_SecondPoint,this->m_SecondPoint);
	rect.InflateRect(3,3);
	pDC->Rectangle(rect);

	rect.SetRect(this->m_FirstPoint.x,this->m_SecondPoint.y,this->m_FirstPoint.x,this->m_SecondPoint.y);
	rect.InflateRect(3,3);
	pDC->Rectangle(rect);

	rect.SetRect(this->m_SecondPoint.x,this->m_FirstPoint.y,this->m_SecondPoint.x,this->m_FirstPoint.y);
	rect.InflateRect(3,3);
	pDC->Rectangle(rect);

	pDC->SelectObject(OldBrush);
	brush.DeleteObject();
	brush.CreateSolidBrush(RGB(255,0,0));
	pDC->SelectObject(&brush);
	rect.SetRect((this->m_SecondPoint.x+this->m_FirstPoint.x)/2,(this->m_SecondPoint.y+this->m_FirstPoint.y)/2,(this->m_SecondPoint.x+this->m_FirstPoint.x)/2,(this->m_SecondPoint.y+this->m_FirstPoint.y)/2);
	rect.InflateRect(3,3);
	pDC->Rectangle(rect);
	pDC->SelectObject(OldPen);
	pDC->SetBkMode(oldBkMode); 
	pDC->SelectObject(OldBrush);
	pDC->SetROP2(oldDrawingMode);


	//CPen m_pen;
	//m_pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
	//CPen *OldPen=pDC->SelectObject(&m_pen);
	//int oldBkMode=pDC->SetBkMode(TRANSPARENT);
	//int OldRop=pDC->SetROP2(R2_NOTXORPEN);   

	//CRect rect;
	//rect.SetRect(m_PositionPoint.x,m_PositionPoint.y,m_PositionPoint.x+m_Width,m_PositionPoint.y+m_Hight);

	//pDC->Rectangle(rect);
	//pDC->SelectObject(OldPen);
	//pDC->SetBkMode(oldBkMode);
	//pDC->SetROP2(OldRop);

}


void CText::DrawActive(CDC* pDC, CPoint point)
{


	CPen m_pen;
	CBrush m_brush;

	m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
	CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
	LOGBRUSH brushlog;
	brushlog.lbColor=m_BrushColor;
	brushlog.lbHatch=m_BrushHatch;
	brushlog.lbStyle=m_BrushStyle;

	m_brush.CreateBrushIndirect(&brushlog);
	SetBrushOrg(pDC,&m_brush);

	CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);

	int OldBkMode=pDC->SetBkMode(m_BkMode);
	COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
	CPoint firstPoint=m_FirstPoint;
	CPoint secondPoint=m_SecondPoint;
	pDC->MoveTo(firstPoint.x,firstPoint.y);
	pDC->LineTo(firstPoint.x,secondPoint.y);
	pDC->LineTo(secondPoint.x,secondPoint.y);
	pDC->LineTo(secondPoint.x,firstPoint.y);
	pDC->LineTo(firstPoint.x,firstPoint.y);
	//13955088899

	pDC->SelectObject(brush);
	pDC->SelectObject(pen);
	pDC->SetBkMode(OldBkMode);
	pDC->SetBkColor(OldColor);
}
